package server.dispatcher;


import common.EnvelopeMessage;
import server.handler.SimpleHandler;
import server.session.Session;

import java.util.HashMap;
import java.util.Map;

public class Dispatcher {


    private final Map<String, Map<String, SimpleHandler>> routes = new HashMap<>();

    public void register(String module, String action, SimpleHandler handler) {
        routes.computeIfAbsent(module, m -> new HashMap<>()).put(action, handler);
    }

    public void dispatch(EnvelopeMessage msg, Session session) {
        session.updateLastActive();
        Map<String, SimpleHandler> moduleMap = routes.get(msg.getModule());
        if (moduleMap == null) return;

        SimpleHandler handler = moduleMap.get(msg.getAction());
        if (handler == null) return;

        handler.handle(msg, session);
    }
}
